package liuds.music.model;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.ImageButton;

import java.io.IOException;
import java.util.List;

import liuds.music.App;
import liuds.music.R;
import liuds.music.utils.MusicUtils;
import liuds.music.utils.Utils;

/**
 * Created by Administrator on 2018/5/7.
 */

public class MediaService extends Service implements MediaPlayer.OnCompletionListener {

    private MediaBinder mediaBinder = new MediaBinder();
    public  MediaPlayer mediaPlayer = new MediaPlayer();

    public boolean isPause = false;

    private OnMediaPlayerStateChangeListener onMediaPlayerStateChangeListener;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mediaBinder;
    }

    @Override
    public void onCreate() {
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
                onMediaPlayerStateChangeListener.onStart();
            }
        });
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int index = intent.getIntExtra("index",0);

        playMusic(index);

        return super.onStartCommand(intent, flags, startId);
    }

    public void playMusic(int index){
        Music currentMusic = MusicUtils.getPlayMusicList().get(MusicUtils.getPlayIndex());
        Music clickMusic = MusicUtils.getPlayMusicList().get(index);
        if(mediaPlayer.isPlaying()){
            if(currentMusic.equals(clickMusic)){
                mediaPlayer.pause();
                isPause = true;
                onMediaPlayerStateChangeListener.onPause();
            }else{
                play(clickMusic,index);
            }
        }else{
            if(isPause){
                if(currentMusic.equals(clickMusic)){
                    mediaPlayer.start();
                    isPause = false;
                    onMediaPlayerStateChangeListener.onStart();
                }else{
                    play(clickMusic,index);
                }
            }else{
                play(clickMusic,index);
            }
        }
    }

    private void play(Music music,int index){
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(music.getFileUrl());
            mediaPlayer.prepareAsync();
            MusicUtils.setPlayIndex(index);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void closeMedia() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        onMediaPlayerStateChangeListener.onPlayCompletion();
    }

    public class MediaBinder extends Binder{

        public MediaService getMediaService(OnMediaPlayerStateChangeListener listener){
            onMediaPlayerStateChangeListener = listener;
            return MediaService.this;
        }
    }

    public interface OnMediaPlayerStateChangeListener{
        void onStart();
        void onPause();
        void onPlayCompletion();
    }
}
